package com.somsoft.handjc.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;

import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.Toast;

import com.somsoft.handjc.activity.jiancha.JianChaActivity;
import com.somsoft.handjc.activity.query.QueryActivity;
import com.somsoft.handjc.activity.rank.RankActivity;
import com.somsoft.handjc.activity.setting.SettingActivity;
import com.somsoft.handjc.activity.tongji.TongJiActivity;
import com.somsoft.handjc.util.MyApplication;
import com.somsoft.handjc.util.XunChaDataUtil;
import com.somsoft.handjc.util.XzspDataUtil;
import com.somsoft.handjc.util.XzzfDataUtil;

public class MainActivity extends TabActivity implements OnCheckedChangeListener{
    /** Called when the activity is first created. */
	private TabHost mHost;
	private RadioGroup radioderGroup;
	private Thread deptThread;
	Handler myHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			if(msg.what==0){
				Toast.makeText(MainActivity.this, "加载部门数据成功！", Toast.LENGTH_SHORT).show();
			}else if(msg.what==1){
				Toast.makeText(MainActivity.this, "加载部门数据失败！", Toast.LENGTH_SHORT).show();
			}
		}		
	};
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.getInstance().addActivity(this);
        requestWindowFeature(Window.FEATURE_NO_TITLE); 
        setContentView(R.layout.maintabs);
        mHost=this.getTabHost();
        
//        mHost.addTab(mHost.newTabSpec("ONE").setIndicator("ONE").setContent(new Intent(this,IndexActivity.class)));
        mHost.addTab(mHost.newTabSpec("TWO").setIndicator("TWO").setContent(new Intent(this,JianChaActivity.class)));
        mHost.addTab(mHost.newTabSpec("THREE").setIndicator("THREE").setContent(new Intent(this,RankActivity.class)));
        mHost.addTab(mHost.newTabSpec("FOUR").setIndicator("FOUR").setContent(new Intent(this,QueryActivity.class)));
        mHost.addTab(mHost.newTabSpec("FIVE").setIndicator("FIVE").setContent(new Intent(this,TongJiActivity.class)));
        mHost.addTab(mHost.newTabSpec("SIX").setIndicator("SIX").setContent(new Intent(this,SettingActivity.class)));
        radioderGroup = (RadioGroup) findViewById(R.id.main_radio);
		radioderGroup.setOnCheckedChangeListener(this);
		
		deptThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                	if(XzzfDataUtil.xzzfDeptNames == null){
						List<String> names = new ArrayList<String>();
						JSONArray array = XzzfDataUtil.getDeptNames();
						for(int i=0;i<array.length();i++){
							names.add(array.getString(i));
						}
						XzzfDataUtil.xzzfDeptNames = names;
					}
                    
                	if(XzspDataUtil.xzspDeptNames == null){
						List<String> names = new ArrayList<String>();
						JSONArray array = XzspDataUtil.getDeptNames();
						for(int i=0;i<array.length();i++){
							names.add(array.getString(i));
						}
						XzspDataUtil.xzspDeptNames = names;
					}
                	
                    if(XunChaDataUtil.xunchaDeptNames == null){
                    	List<String> names = new ArrayList<String>();
						JSONArray array = XunChaDataUtil.getDeptNames();
						for(int i=0;i<array.length();i++){
							names.add(array.getString(i));
						}
						XunChaDataUtil.xunchaDeptNames = names;
                    }
                    
                    if(XunChaDataUtil.xunchaLeaderNames == null){
                    	List<String> names = new ArrayList<String>();
						JSONArray array = XunChaDataUtil.getLeaderNames();
						for(int i=0;i<array.length();i++){
							names.add(array.getString(i));
						}
						XunChaDataUtil.xunchaLeaderNames = names;
                    }
                    Log.d("初始数据", "获取成功");
                } catch (Exception e) {
                	Toast.makeText(MainActivity.this, "加载部门数据失败！", Toast.LENGTH_SHORT).show();
                	myHandler.sendEmptyMessage(1);
                }
            }
        });
		deptThread.start(); /* 启动线程 */

    }
	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch(checkedId){
//		case R.id.radio_button0:
//			mHost.setCurrentTabByTag("ONE");
//			break;
		case R.id.radio_button1:
			((RadioButton)findViewById(R.id.radio_button1)).setTextColor(Color.rgb(18, 68, 90));
			((RadioButton)findViewById(R.id.radio_button2)).setTextColor(Color.WHITE);
			((RadioButton)findViewById(R.id.radio_button3)).setTextColor(Color.WHITE);
			((RadioButton)findViewById(R.id.radio_button4)).setTextColor(Color.WHITE);
			((RadioButton)findViewById(R.id.radio_button5)).setTextColor(Color.WHITE);
			mHost.setCurrentTabByTag("TWO");
			break;
		case R.id.radio_button2:
			((RadioButton)findViewById(R.id.radio_button1)).setTextColor(Color.WHITE);
			((RadioButton)findViewById(R.id.radio_button2)).setTextColor(Color.rgb(18, 68, 90));
			((RadioButton)findViewById(R.id.radio_button3)).setTextColor(Color.WHITE);
			((RadioButton)findViewById(R.id.radio_button4)).setTextColor(Color.WHITE);
			((RadioButton)findViewById(R.id.radio_button5)).setTextColor(Color.WHITE);
			mHost.setCurrentTabByTag("THREE");
			break;
		case R.id.radio_button3:
			((RadioButton)findViewById(R.id.radio_button1)).setTextColor(Color.WHITE);
			((RadioButton)findViewById(R.id.radio_button2)).setTextColor(Color.WHITE);
			((RadioButton)findViewById(R.id.radio_button3)).setTextColor(Color.rgb(18, 68, 90));
			((RadioButton)findViewById(R.id.radio_button4)).setTextColor(Color.WHITE);
			((RadioButton)findViewById(R.id.radio_button5)).setTextColor(Color.WHITE);
			mHost.setCurrentTabByTag("FOUR");
			break;
		case R.id.radio_button4:
			((RadioButton)findViewById(R.id.radio_button1)).setTextColor(Color.WHITE);
			((RadioButton)findViewById(R.id.radio_button2)).setTextColor(Color.WHITE);
			((RadioButton)findViewById(R.id.radio_button3)).setTextColor(Color.WHITE);
			((RadioButton)findViewById(R.id.radio_button4)).setTextColor(Color.rgb(18, 68, 90));
			((RadioButton)findViewById(R.id.radio_button5)).setTextColor(Color.WHITE);
			mHost.setCurrentTabByTag("FIVE");
			break;
		case R.id.radio_button5:
			((RadioButton)findViewById(R.id.radio_button1)).setTextColor(Color.WHITE);
			((RadioButton)findViewById(R.id.radio_button2)).setTextColor(Color.WHITE);
			((RadioButton)findViewById(R.id.radio_button3)).setTextColor(Color.WHITE);
			((RadioButton)findViewById(R.id.radio_button4)).setTextColor(Color.WHITE);
			((RadioButton)findViewById(R.id.radio_button5)).setTextColor(Color.rgb(18, 68, 90));
			mHost.setCurrentTabByTag("SIX");
			break;
		}		
	}
}